package net.luohuasheng.bee.proxy.mybatis.common.utils;

import net.luohuasheng.bee.proxy.mybatis.executor.page.Page;
import net.luohuasheng.bee.proxy.mybatis.executor.page.Pageable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wusuoming
 */
public class PageUtils {
    public static <Source, Target> Page<Target> loadNewPage(Page<Source> page, ObjectConverter<Source, Target> objectConverter) {
        List<Target> content = new ArrayList<>();
        for (Source row : page.getContent()) {
            content.add(objectConverter.convert(row));
        }
        int row = (int) page.getTotal(), page2 = 0;
        if (page.isFirst() && page.isLast()) {
            page2 = (int) page.getTotal();
        } else if (!page.isLast()) {
            page2 = 1;
        } else {
            page2 = 2;
            row = 1;
        }
        return new Page<>(page.getTotal(), content, Pageable.of(page2, row));
    }
}
